import java.io.File;

/**
 * 
 * @author Dennis Meyer, Sebastian Brodehl, Sebastian Schappert
 * @version 0.0.0.1
 *
 */
public class CurriculumVitaeI 
{
	private final String REGEXGERMANPHONENUMBER 
	= "^(((((((00|\\+)49[ \\-/]?)|0)[1-9][0-9]{1,4})[ \\-/]?)|((((00|\\+)49\\()|\\(0)[1-9][0-9]{1,4}\\)[ \\-/]?))[0-9]{1,7}([ \\-/]?[0-9]{1,5})?)$";
	
	private final String REGEXEMAIL
	= "\\b[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b";
	
	/**
	 * Convert Umlaut in Latex Umlaut.
	 * 
	 * @param textWithUmlaut the text with umlaut
	 * @return equal text without umlaut.
	 */
	private String convertUmlaut(String textWithUmlaut)
	{
		StringBuilder textWithConvertUmlaut = new StringBuilder();
		char[] tempTextWithUmlaut = textWithUmlaut.toLowerCase().toCharArray();
		for (char letter : tempTextWithUmlaut) 
		{
			switch (letter)
			{
			case 'ö':
			case 'ä':
			case 'ü':
				textWithConvertUmlaut.append('\"');
				textWithConvertUmlaut.append(letter);
				break;
			case 'ß':
				textWithConvertUmlaut.append("\\ss");
				break;
				
			default:
				textWithConvertUmlaut.append(letter);
			}
		}
		
		return textWithConvertUmlaut.toString();
	}

	/**
	 * Create a string wtih personaldata and give it back.
	 * 
	 * @param givenName
	 * @param lastName
	 * @param picturePath
	 * @return a formatted text, wich latex can read.
	 */
	private String createPersonalData(String givenName, String lastName, String picturePath)
	{
		try
		{
			// Check given data
			if (givenName == "" || lastName == "")
				throw new Exception("MissingName");
			return "";
			
		}catch (Exception ex)
		{
			System.out.println("Error: " + ex.getMessage());
		}
		
		StringBuilder personalData = new StringBuilder();
		// Create GivenName
		personalData.append("\\firstname{");
		personalData.append(givenName);
		personalData.append("}/n");
		
		// Create LastName
		personalData.append("\\familyname{");
		personalData.append(lastName);
		personalData.append("}/n");

		File tempPictureFile = new File(picturePath);
		if (!tempPictureFile.exists())
		{
			// Create PicturePath 
			personalData.append("\\photo [96 pt ]{");
			personalData.append(picturePath);
			personalData.append("}/n");
		}
		
		return personalData.toString();
	}

	
	/**
	 * Create a mobileline in latex format.
	 * 
	 * @param mobileNumber
	 * @return the formatted mobileLine.
	 */
	private String createMobileLine(String mobileNumber)
	{
		try
		{
			// Check given string
			if (!mobileNumber.matches(REGEXGERMANPHONENUMBER))
				throw new Exception("InvalidMobileNumber");
		}
		catch (Exception ex)
		{
			System.out.println("Error: " + ex.getMessage());
			return "";
		}
		
		StringBuilder mobileLineBuilder = new StringBuilder();
		mobileLineBuilder.append("cvline{\\mobilesymbol}{");
		mobileLineBuilder.append(mobileNumber);
		mobileLineBuilder.append("}");
		
		return mobileLineBuilder.toString();
	}

	/**
	 * Create a emailline in latex format.
	 * 
	 * @param emailLine
	 * @return the formatted emailline.
	 */
	private String createEmailLine(String emailLine)
	{	
		try
		{
			// Check given string
			if (!emailLine.matches(REGEXEMAIL))
				throw new Exception("InvalidEmail");
		}
		catch (Exception ex)
		{
			System.out.println("Error: " + ex.getMessage());
			return "";
		}
		
		StringBuilder emailLineBuilder = new StringBuilder();
		emailLineBuilder.append("\\cvline{\\emailsymbol}{\\href{mailto : ");
		emailLineBuilder.append(emailLine);
		emailLineBuilder.append("}{");
		emailLineBuilder.append(emailLine);
		emailLineBuilder.append("}}");
		
		return emailLineBuilder.toString();
	}

	/**
	 * Create a cvline for latex.
	 * 
	 * @param first 
	 * @param second
	 * @return the cvline
	 */
	private String createCVLine(String first, String second)
	{
		try
		{
			// Check given string
			if (second.equals(""))
				throw new Exception("InvalidCVLine");
		}
		catch (Exception ex)
		{
			System.out.println("Error: " + ex.getMessage());
			return "";
		}
		
		StringBuilder createCVLineBuilder = new StringBuilder();
		createCVLineBuilder.append("\\cvline{");
		createCVLineBuilder.append(first);
		createCVLineBuilder.append("}{");
		createCVLineBuilder.append(second);
		createCVLineBuilder.append("}");
		
		return  createCVLineBuilder.toString();
	}

	/**
	 * Create a cventry for latex.
	 * 
	 * @param cvEntryArray
	 * @return the cventry
	 */
	private String createCVEntry(String[] cvEntryArray)
	{
		try
		{
			// Check given string
			if (cvEntryArray[0].equals("") || cvEntryArray[1].equals(""))	
				throw new Exception("InvalidCVEntry");
		}
		catch (Exception ex)
		{
			System.out.println("Error: " + ex.getMessage());
			return "";
		}
		
		StringBuilder createCVEntryBuilder = new StringBuilder();
		createCVEntryBuilder.append("\\cventry{");
		for (int i = 0; i < cvEntryArray.length; i++) 
		{
			createCVEntryBuilder.append(cvEntryArray[i]);
			createCVEntryBuilder.append("}{");
		}
		createCVEntryBuilder.append("}");
		
		
		return createCVEntryBuilder.toString();
	}
}
